<?php
/**
 * Created by PhpStorm.
 * User: 罗俊阳
 * Date: 2018/5/5 0005
 * Time: 09:23
 */

namespace app\admin\controller;


use app\common\model\Tenant;
use app\common\model\Tenantdistributiontype;
use controller\BasicAdmin;
use think\Request;
use traits\controller\jump;


/**
 * 物流设置
 * Class Logistics
 * @package app\admin\controller
 *
 */
//class Logistics extends BasicAdmin
class Logistics extends Base
{

    public function findLogistical($logistics_type_id){
       $logisticalModel = new \app\admin\model\Logistical();
       return $logisticalModel->findTypeLogistical($logistics_type_id);
    }

    public function getType($logistics_type_id){
        $typeModel = new Tenantdistributiontype();
        return $typeModel->findDistributionType($logistics_type_id);
    }

    public function index(){
        $tenantId = session("TENANT_ID");
        $logisticsModel = new \app\common\model\Logistics();
        $logistics_type_id = Request::instance()->param('logistics_type_id');
        $getLogistics = $logisticsModel->findLogistics($logistics_type_id);
        $getType = self::getType($logistics_type_id);
        if($getLogistics && !empty($getLogistics)){
            $distanceIncrease = $getLogistics->distanceIncrease;                 //距离加价
            $distanceIncrease = json_decode($distanceIncrease,true);

            $nocturnalPriceIncrease = $getLogistics->nocturnalPriceIncrease;   //夜间加价
            $nocturnalPriceIncrease  = json_decode($nocturnalPriceIncrease,true);

            $weightIncrease = $getLogistics->weightIncrease;                    //重量加价
            $weightIncrease  = json_decode($weightIncrease,true);


            $packingCharge = $getLogistics->packingCharge/100;                       //打包费
            $freight = $getLogistics->freight/100;                                  //运费
            $status = $getLogistics->status;                                    //状态
            $orderStartingPrice = $getLogistics->orderStartingPrice/100;            //订单起送价
            $deliverMethod = $getLogistics->deliverMethod;




            $this->assign('packingCharge',isset($packingCharge)?$packingCharge:"");//包装费

            $this->assign('status',isset($status)?$status:0);                //状态

            $this->assign('orderStartingPrice',isset($orderStartingPrice)?$orderStartingPrice:"");   //订单起送价

            $this->assign('freight',isset($freight)?$freight:"");                         //运费

            //距离加价
            $this->assign('distanceIncrease_1_3',isset($distanceIncrease['distanceIncrease_1_3_val'])?$distanceIncrease['distanceIncrease_1_3_val']/100:"");
            $this->assign('distanceIncrease_3_5',isset($distanceIncrease['distanceIncrease_3_5_val'])?$distanceIncrease['distanceIncrease_3_5_val']/100:"");

            //重量加价
            $this->assign('weightIncrease_1',isset($weightIncrease['weightIncrease_1_val'])?$weightIncrease['weightIncrease_1_val']/100:"");
            $this->assign('weightIncrease_1_5',isset($weightIncrease['weightIncrease_1_5_val'])?$weightIncrease['weightIncrease_1_5_val']/100:"");
            $this->assign('weightIncrease_5_10',isset($weightIncrease['weightIncrease_5_10_val'])?$weightIncrease['weightIncrease_5_10_val']/100:"");

            //夜间加价
            $this->assign('nocturnalPriceIncrease_22_08',isset($nocturnalPriceIncrease['nocturnalPriceIncrease_22_08_val'])?$nocturnalPriceIncrease['nocturnalPriceIncrease_22_08_val']/100:"");

            //商家配送数据
            $this->assign('getLogistics',$getLogistics);


        }
        /***************************    start物流配送规则和费用获取   *******************************/

        $logisticalResult = self::findLogistical($logistics_type_id);
        if(!empty($logisticalResult)){
            $logisticalResult->packingCharge = $logisticalResult->packingCharge/100;
            $logisticalRrule = $logisticalResult->logsticalRrule;
            $logisticalRrule->unifiedmoney = $logisticalRrule->unifiedmoney/100;
            $logisticalRrule->first_money = $logisticalRrule->first_money/100;
            $logisticalRrule->continued_money = $logisticalRrule->continued_money/100;

            $this->assign("logisticalResult",$logisticalResult);
            $this->assign("logisticalRrule",$logisticalRrule);
            $this->assign("logisticalId",isset($logisticalResult->id)?$logisticalResult->id:"");
        }

        /**************************   end    *****************************
         */
        $this->assign('deliverMethod',isset($deliverMethod)?$deliverMethod:"");//配送方式

        //获取物流公司列表
            $logisticsList = self::LogisticsCode();
            $this->assign("logisticsList",$logisticsList);
            $this->assign('logistics_type_id',$logistics_type_id);
            $this->assign('getType',$getType);
            return $this->fetch();
    }


    public function ajax_Add_Or_update_logistics(){
        $tenantId = session("TENANT_ID");
        $is_enable = input("is_enable");
        $orderStartingPrice = input("orderStartingPrice")?input("orderStartingPrice"):0;
        $freight = input("freight")?input("freight"):0;
        $packingCharge = input("packingCharge")?input("packingCharge"):0;
        $orderStartingPrice = $orderStartingPrice * 100;
        $freight = $freight * 100;
        $packingCharge = $packingCharge * 100;

        //夜间加价
        $nocturnalPriceIncrease = input("nocturnalPriceIncrease");
        $weightIncrease = input("weightIncrease");
        $deliverMethod = input("deliverMethod");
        $distanceIncrease = input("distanceIncrease");
        //夜间加价
        $nocturnalPriceIncreaseData['nocturnalPriceIncrease'] = $nocturnalPriceIncrease;
        $tdtTypeId =  input("tdtTypeId");
        $data['weightIncrease'] = $weightIncrease;
        $data['orderStartingPrice'] = $orderStartingPrice;   //订单起送价格
        $data['freight'] = $freight;    //物流费
        $data['distanceIncrease'] = $distanceIncrease;       //距离 加价
        $data['nocturnalPriceIncrease'] = $nocturnalPriceIncrease;  //夜间 加价
        $data['packingCharge'] = $packingCharge;  //打包费
        $data['status']=$is_enable; //是否启用
        $data['tenantId']=$tenantId;
        $data['tenantDistributionId']=$tdtTypeId;
        $data['deliverMethod'] = $deliverMethod;
        $logisticsModel = new \app\common\model\Logistics();
        $logistics = $logisticsModel->findLogistics($tdtTypeId);
        if($logistics && !empty($logistics)){
            $res = $logisticsModel->updateLogistics($data,$tdtTypeId);
            //修改type配送方式和状态
            if($res){
                //修改物流配送状态
                $logisticalModel = new \app\admin\model\Logistical();
                $logisticalParam['tenantDistributionId'] = $tdtTypeId;
                $logisticalParam['status'] = $logisticalModel::STATUS_INVALID;
                $logisticalRes = $logisticalModel->updateLogistical($logisticalParam);
                if(!$logisticalRes){
                    return show($logisticsModel::STATUS_INVALID,"修改物流配送状态失败",$logisticalRes);
                }
                $typeParam['id'] = $tdtTypeId;
                $typeParam['deliverMethod'] = $deliverMethod;
                $typeParam['status'] = $is_enable;
                $typeResult = self::updateTypeStatus($typeParam);
                if(!$typeResult){
                    return show($logisticsModel::STATUS_INVALID,"修改物流状态失败",$typeResult);
                }

            }

            if($res){
                $msg = "修改成功";
            }else{
                $msg = "修改失败";
            }
        }else{
            $res = $logisticsModel->tdAdd($data,$tdtTypeId);
            if($res){
                $msg = "新增成功";
            }else{
                $msg = "新增失败";
            }
        }

        return show($logisticsModel::STATUS_INVALID,$msg,$res);
    }

    public function closeStatus() {
        $request = Request::instance();
        $id = $request->param('id');
        $update = Tenantdistributiontype::where('id',$id)->update(['status'=>0]);
        if($update) {
            return show('1','修改成功',$update);
        }
        return show('2','请求失败',$update);
    }

    private function updateTypeStatus($param){
       $typeModel = new Tenantdistributiontype();
        return $typeModel->updateDistributionType($param);
    }

    public function ajax_get_stores(){




    }



    /**
     * 获取物流列表物流公司编号
     */
    public function LogisticsCode(){


        $ZTO['name'] = "中通快递";
        $ZTO['code'] = "ZTO";
        $data['ZTO'] = $ZTO;


        $YTO['name'] = "圆通速递";
        $YTO['code'] = "YTO";
        $data["YTO"] = $YTO;

        $YD['name'] = "韵达速递";
        $YD['code'] = "YD";
        $data["YD"] = $YD;

        $YZPY['name'] = "邮政快递包裹";
        $YZPY['code'] = "YZPY";
        $data["YZPY"] = $YZPY;

        $EMS['name'] = "EMS";
        $EMS['code'] = "EMS";
        $data["EMS"] = $EMS;

        $HHTT['name'] = "天天快递";
        $HHTT['code'] = "HHTT";
        $data["HHTT"] = $HHTT;

        $JD['name'] = "京东物流";
        $JD['code'] = "JD";
        $data["JD"] = $JD;

        $UC['name'] = "优速快递";
        $UC['code'] = "UC";
        $data["UC"] = $UC;

        $DBL['name'] = "德邦快递";
        $DBL['code'] = "DBL";
        $data["DBL"] = $DBL;

        $FAST['name'] = "快捷快递";
        $FAST['code'] = "FAST";
        $data["FAST"] = $FAST;

        $ZJS['name'] = "宅急送";
        $ZJS['code'] = "ZJS";
        $data["ZJS"] = $ZJS;

        $TNT['name'] = "TNT快递";
        $TNT['code'] = "TNT";
        $data["TNT"] = $TNT;

        return $data;
    }




    //电子面单
    public function createLogistics(){


    }




}